package com.mes.warehouse.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.common.pojo.MESResult;
import com.mes.common.util.Constant;
import com.mes.pojo.warehouse.WarehouseArea;
import com.mes.warehouse.mapper.WarehouseAreaMapper;
import com.mes.warehouse.mapper.WarehouseMapper;
import com.mes.warehouse.server.WarehouseAreaServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 张喆
 * @Company
 * @Description
 * @Time 2024/10/25 9:32
 */
@Service
public class WarehouseAreaServerImpl implements WarehouseAreaServer {
    @Autowired
    private WarehouseAreaMapper warehouseAreaMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;

    @Override
    public MESResult queryAll(WarehouseArea warehouseArea, Integer pageNum, Integer limit) {
        PageHelper.startPage(pageNum,limit);
        List<WarehouseArea> warehouseAreas = warehouseAreaMapper.selectAll(warehouseArea);
        PageInfo<WarehouseArea> warehouseAreaPageInfo = new PageInfo<>(warehouseAreas);
        return MESResult.success(warehouseAreaPageInfo);
    }

    @Override
    public MESResult queryById(String warehouseAreaId, String warehouseId) {
        return MESResult.success(warehouseAreaMapper.selectById(warehouseAreaId,warehouseId));
    }

    @Override
    public MESResult deleteWarehouseArea(String warehouseAreaId, String warehouseId) {
        WarehouseArea warehouseArea = warehouseAreaMapper.selectById(warehouseAreaId, warehouseId);
        warehouseArea.setWarehouseAreaStatus(99);
        return MESResult.success(warehouseAreaMapper.updateWarehouseArea(warehouseArea));
    }

    @Override
    public MESResult updateWarehouseArea(WarehouseArea warehouseArea) {
        WarehouseArea same = warehouseAreaMapper.selectByName(warehouseArea.getWarehouseAreaName(), warehouseArea.getWarehouseId());
        if (same != null && !same.getWarehouseAreaId().equals(warehouseArea.getWarehouseAreaId())){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        // 若当前仓库并未进行使用，查询结果为bull，手动进行赋值
        Long areaInUse = warehouseAreaMapper.countTotalArea(warehouseArea.getWarehouseId());
        if (areaInUse == null){
            areaInUse = 0L;
        }
        if ((warehouseArea.getWarehouseAreaArea()
                + areaInUse
                //减去原本的面积占地
                - same.getWarehouseAreaArea()
        )
                > warehouseMapper.selectById(warehouseArea.getWarehouseId()).getWarehouseArea()){
            return MESResult.error(Constant.RESPONSE_STATUS_ERROR);
        }
        return MESResult.success(warehouseAreaMapper.updateWarehouseArea(warehouseArea));
    }

    @Override
    public MESResult insertWarehouseArea(WarehouseArea warehouseArea) {
        WarehouseArea same = warehouseAreaMapper.selectByName(warehouseArea.getWarehouseAreaName(), warehouseArea.getWarehouseId());
        if (same != null){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        // 若当前仓库并未进行使用，查询结果为bull，手动进行赋值
        Long areaInUse = warehouseAreaMapper.countTotalArea(warehouseArea.getWarehouseId());
        if (areaInUse == null){
            areaInUse = 0L;
        }
        if ((warehouseArea.getWarehouseAreaArea()
                + areaInUse)
                > warehouseMapper.selectById(warehouseArea.getWarehouseId()).getWarehouseArea()){
            return MESResult.error(Constant.RESPONSE_STATUS_ERROR);
        }
        String maxId = warehouseAreaMapper.selectMaxId();
        String[] split = maxId.split("_");
        int num = Integer.parseInt(split[1]);
        num = num + 1;
        String strNum = null;
        if (num > 0 && num < 10){
            strNum = "000" + num;
        }else if (num < 100){
            strNum = "00" + num;
        }else if (num < 1000){
            strNum = "0" + num;
        }else {
            strNum = num + "";
        }
        warehouseArea.setWarehouseAreaId(split[0] + "_" + strNum);
        return MESResult.success(warehouseAreaMapper.insertWarehouseArea(warehouseArea));
    }
}
